<template>
	<view class="content">
		<u-navbar :fixed="false" title="上传检查问题" :autoBack="true" :safeAreaInsetTop="true" leftIconColor="#fff">
		</u-navbar>

		<scroll-view class="srv" :scroll-y="true">
			<u--form labelPosition="left" :model="formData" labelWidth="100" :rules="rules" ref="uForm">
				<u-form-item label="本项扣分" prop="Score" borderBottom required>
					<u--input :value="formData.Score" border="none" :readonly="true"></u--input>
				</u-form-item>
				<u-form-item label="责任人员" prop="ToUserName" borderBottom required @click="openBaseScreen('责任人员')">
					<u--input v-model="formData.ToUserName" disabled disabledColor="#ffffff" placeholder="请选择责任人员"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="问题描述" prop="Description" borderBottom required>
					<u--textarea v-model="formData.Description" :count="true" :autoHeight="true" border="bottom"
						:maxlength="500" placeholder="请输入问题描述"></u--textarea>
				</u-form-item>
				<u-form-item label="现场照片" prop="DImgs" labelPosition="top" borderBottom required>
					<view style="padding-top: 10px;">
						<u-upload :fileList="DImgs" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="9"
							:previewFullImage="true"></u-upload>
					</view>
				</u-form-item>
			</u--form>
		</scroll-view>

		<view class="bottom">
			<u-button type="primary" color="#51d0d0" text="提交" @click="confirmSubmit"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				IsSerious: 0,
				formData: {
					DailyFSDeID: 0,
					Score: 0,
					ToUserID: 0,
					ToUserName: '',
					Description: '',
					DImgs: ''
				}, // 表单内容
				DImgs: [],
				rules: {
					'ToUserName': {
						type: 'string',
						required: true,
						message: '请选择责任人员',
						trigger: ['blur', 'change']
					},
					'Description': {
						type: 'string',
						required: true,
						message: '请填写问题描述',
						trigger: ['blur', 'change']
					},
					'DImgs': {
						type: 'string',
						required: true,
						message: '请上传现场照片',
						trigger: ['blur', 'change']
					}
				} // 表单验证规则
			};
		},
		created() {

		},
		mounted() {
			let that = this;
			uni.$on('CheckSubmit_Choice', function(ret) {
				if (ret.type == '责任人员') {
					that.formData.ToUserID = ret.data.UserID;
					that.formData.ToUserName = ret.data.UserName;
				}
			})
		},
		methods: {

			// 删除图片
			deletePic(event) {
				this.DImgs.splice(event.index, 1);
			},

			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.DImgs.length;
				lists.map((item) => {
					this.DImgs.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})

				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.DImgs[fileListLen]
					this.DImgs.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},

			uploadFilePromise(url) {
				let that = this;
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://' + that.Request
							.serverIP() + '/Frame/uploadFile?dirName=' + '5S图片',
						filePath: url,
						name: 'file',
						header: {
							'Content-Type': 'multipart/form-data' // 必须设置
						},
						formData: {
							user: 'test'
						},
						success: (uploadFileRes) => {
							const data = JSON.parse(uploadFileRes.data);
							resolve(data.data)
						}
					});
				})
			},

			// 选择责任人员
			openBaseScreen(str) {
				let obj = {};

				if (str == '责任人员') {
					obj = {
						allowClear: false,
						cmd: '5S_getCkUserList',
						type: str,
						ListeningName: 'CheckSubmit_Choice',
						serverSearch: {
							isPaging: false, //是否分页
							searchKey: 'SearchKey', //搜索关键词，应包含在param中
							showText: 'UserName', //查询结果显示的字段
							pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中
							// subText: [{
							// 	label: '描述',
							// 	labelKey: 'desc'
							// }],
							ChoiceDataKey: 'UserID',
							ChoiceDataData: this.formData.ToUserID,
						},
						param: {
							SearchKey: '',
							// PageIndex: 1,
							// PageSize: 30
						}
					}
				}

				this.$store.commit('ChangeAndSetStateData', {
					Name1: 'BaseScreenParm',
					data: obj
				})

				uni.navigateTo({
					url: '/commonPage/Tool/baseScreen'
				})
			},

			// 5S检查提交
			confirmSubmit() {
				let that = this;
				if (that.DImgs.length) {
					that.formData.DImgs = that.DImgs.map(item => item.url).join(',');
				}
				console.log(that.formData);
				that.$refs.uForm.validate().then(res => {
					that.Request.request('5S_addDeal', that.formData, function(data) {
							uni.showToast({
								title: '问题上传成功!'
							})
							that.formData.ToUserID = 0;
							that.formData.ToUserName = '';
							that.formData.Description = '';
							that.formData.DImgs = '';
							that.DImgs = [];
						}, function(err) {}, that.Request.IPAgreement() + that.Request.serverIP() +
						'/Frame/SqlData', 1)
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		},
		onLoad(option) {
			this.IsSerious = option.IsSerious;
			this.formData.DailyFSDeID = option.DailyFSDeID;
			this.formData.Score = this.IsSerious == 1 ? 5 : 2;
		}
	}
</script>

<style lang="scss" scoped>
	.u-form {
		.u-form-item {
			background-color: #fff;
			padding: 0 16px;
		}
	}

	.content {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.srv {
			display: flex;
			flex-direction: column;
			height: calc(100vh - (44px + 50px + env(safe-area-inset-top) + env(safe-area-inset-bottom) / 2));
			padding: 10px;
		}

		.bottom {
			display: flex;
			justify-content: center;
			width: 100vw;
			height: 50px;

			button {
				width: 88vw;
			}
		}
	}
</style>